package cn.iocoder.yudao.module.order.controller.admin.tradeorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.order.controller.admin.tradeorder.vo.*;
import cn.iocoder.yudao.module.order.dal.dataobject.tradeorder.TradeOrderDO;
import cn.iocoder.yudao.module.order.convert.tradeorder.TradeOrderConvert;
import cn.iocoder.yudao.module.order.service.tradeorder.TradeOrderService;

/**
 * @author Administrator
 */
@Tag(name = "管理后台 - 交易订单")
@RestController
@RequestMapping("/order/trade-order")
@Validated
public class TradeOrderController {

    @Resource
    private TradeOrderService tradeOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建交易订单")
    @PreAuthorize("@ss.hasPermission('order:trade-order:create')")
    public CommonResult<Integer> createTradeOrder(@Valid @RequestBody TradeOrderCreateReqVO createReqVO) {
        return success(tradeOrderService.createTradeOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新交易订单")
    @PreAuthorize("@ss.hasPermission('order:trade-order:update')")
    public CommonResult<Boolean> updateTradeOrder(@Valid @RequestBody TradeOrderUpdateReqVO updateReqVO) {
        tradeOrderService.updateTradeOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除交易订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:trade-order:delete')")
    public CommonResult<Boolean> deleteTradeOrder(@RequestParam("id") Integer id) {
        tradeOrderService.deleteTradeOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得交易订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:trade-order:query')")
    public CommonResult<TradeOrderRespVO> getTradeOrder(@RequestParam("id") Integer id) {
        TradeOrderDO tradeOrder = tradeOrderService.getTradeOrder(id);
        return success(TradeOrderConvert.INSTANCE.convert(tradeOrder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得交易订单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:trade-order:query')")
    public CommonResult<List<TradeOrderRespVO>> getTradeOrderList(@RequestParam("ids") Collection<Integer> ids) {
        List<TradeOrderDO> list = tradeOrderService.getTradeOrderList(ids);
        return success(TradeOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得交易订单分页")
    @PreAuthorize("@ss.hasPermission('order:trade-order:query')")
    public CommonResult<PageResult<TradeOrderRespVO>> getTradeOrderPage(@Valid TradeOrderPageReqVO pageVO) {
        PageResult<TradeOrderDO> pageResult = tradeOrderService.getTradeOrderPage(pageVO);
        return success(TradeOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出交易订单 Excel")
    @PreAuthorize("@ss.hasPermission('order:trade-order:export')")
    @OperateLog(type = EXPORT)
    public void exportTradeOrderExcel(@Valid TradeOrderExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TradeOrderDO> list = tradeOrderService.getTradeOrderList(exportReqVO);
        // 导出 Excel
        List<TradeOrderExcelVO> datas = TradeOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "交易订单.xls", "数据", TradeOrderExcelVO.class, datas);
    }

}
